//
//  HZChartView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/14.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZChartView.h"
#import "MYRadioGroup.h"
#import "MinChart.h"
#import "KLineChart.h"
#import "HZMingXiView.h"
#import "MYPortraitRadioGroup.h"

#define kChartViewH 300
#define kMenuH 30

@interface HZChartView()
{
    NSArray *_mingxiCache;
}
@property (nonatomic, strong)MYRadioGroup  *pRadioGroup;
@property (nonatomic, strong)MYPortraitRadioGroup *pPortraitRadioGroup;
@property (nonatomic, strong)UIButton             *pBackView;
@property (nonatomic, strong)MinChart      *pMinChart;
@property (nonatomic, strong)KLineChart    *pKlineChart;
@property (nonatomic, strong)HZMingXiView  *pMingXiView;
@property (nonatomic, strong)UIView *pCurrentShowView;
@property (nonatomic, assign)NSInteger nMenuState;
@property (nonatomic, assign)kHZSingleStockState nScreenState;
@end

@implementation HZChartView

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}

#pragma mark -
#pragma mark public
- (void)showKlineIndex:(int)index
{
    [_pKlineChart setSubChartIndex:index];
}
- (void)updateKline
{
    [_pKlineChart updateNewData];
}

#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pRadioGroup = [MYRadioGroup new];
    _pRadioGroup.parent = self;
    _pRadioGroup.backgroundColor = [HZCommonTool getThemeColorWithDefaultColor:kMYHexToRGB(0xe8e8e8, 1)];
    NSArray *titleArray = [NSArray arrayWithObjects:
                           kHZFenShi,
                           kHZMingXi,
                           kHZDayK,
                           kHZWeekK,
                           kHZMonthK,
                           kHZMinute, nil];
    [_pRadioGroup setRadioGroup:titleArray];
    [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:NO];
    [_pRadioGroup setSelectedButton:0];
    
    
    
    [self addSubview:_pRadioGroup];
    
    [self p_initConstraints];
    
    [self p_loadKLineChartView];
    _pCurrentShowView = [self p_loadMinChartView];
    _nMenuState = 0;
    _nScreenState = kHZSingleStock_portrait_State;
}

- (void)p_initConstraints
{
    [_pRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(kMenuH);
    }];
}

- (void)p_loadPortraitRadioGroup
{
    if (!_pPortraitRadioGroup) {
        _pBackView = [UIButton new];
        _pPortraitRadioGroup = [MYPortraitRadioGroup new];
        _pPortraitRadioGroup.parent = self;
        _pPortraitRadioGroup.layer.borderWidth = 0.5;
        _pPortraitRadioGroup.layer.borderColor = [kMYHexToRGB(0xdddddd, 1) CGColor];
        NSArray *titleArray = [NSArray arrayWithObjects:
                               kHZMinute5,
                               kHZMinute15,
                               kHZMinute30,
                               kHZMinute60, nil];
        [_pPortraitRadioGroup setRadioGroup:titleArray];
        _pBackView.backgroundColor = [UIColor clearColor];
        [_pBackView addTarget:self action:@selector(backViewOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_pBackView];
        [self addSubview:_pPortraitRadioGroup];
        [_pBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        [_pPortraitRadioGroup mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pRadioGroup.mas_bottom);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(kMenuH*4);
            make.width.mas_equalTo(self).multipliedBy(0.2);
        }];
    }
    [self bringSubviewToFront:_pBackView];
    [self bringSubviewToFront:_pPortraitRadioGroup];
    _pBackView.hidden = _pPortraitRadioGroup.hidden = NO;
}

- (UIView *)p_loadMinChartView
{
    if (!_pMinChart) {
        _pMinChart = [MinChart new];
        _pMinChart.parent = self;
        [_pMinChart setMode:MIN_SHOW_MODE_NORMAL];
        [self addSubview:_pMinChart];
        [_pMinChart mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pRadioGroup.mas_bottom);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self);
            make.height.mas_equalTo(kChartViewH);
        }];
    }
    return _pMinChart;
}

- (UIView *)p_loadKLineChartView
{
    if (!_pKlineChart) {
        _pKlineChart = [KLineChart new];
        _pKlineChart.parent = self;
        _pKlineChart.hidden = YES;
        [_pKlineChart setMode:MIN_SHOW_MODE_NORMAL];
        [self addSubview:_pKlineChart];
        if (_nScreenState == kHZSingleStock_landscape_State) {
            CGFloat fChartH = kScreenH - 45 - 45;
            [_pKlineChart mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self);
                make.left.mas_equalTo(self);
                make.right.mas_equalTo(self);
                make.height.mas_equalTo(fChartH);
            }];
        }else{
            [_pKlineChart mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_pRadioGroup.mas_bottom);
                make.left.mas_equalTo(self);
                make.right.mas_equalTo(self);
                make.bottom.mas_equalTo(self);
                make.height.mas_equalTo(kChartViewH);
            }];
        }
    }
    return _pKlineChart;
}

- (UIView *)p_loadMingXiView
{
    if (!_pMingXiView) {
        _pMingXiView = [HZMingXiView new];
        _pMingXiView.parent = self;
        [self addSubview:_pMingXiView];
        if (_nScreenState == kHZSingleStock_landscape_State) {
            CGFloat fChartH = kScreenH - 45 - 45;
            [_pMingXiView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self);
                make.left.mas_equalTo(self);
                make.right.mas_equalTo(self);
                make.height.mas_equalTo(fChartH);
            }];
        }else {
            [_pMingXiView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(_pRadioGroup.mas_bottom);
                make.left.mas_equalTo(self);
                make.right.mas_equalTo(self);
                make.bottom.mas_equalTo(self);
                make.height.mas_equalTo(kChartViewH);
            }];
        }
    }
    return _pMingXiView;
}

- (void)p_showRelatedView:(NSString *)title
{
    [self p_loadMinChartView].hidden = [self p_loadKLineChartView].hidden = [self p_loadMingXiView].hidden = YES;
    if ([title isEqual:kHZFenShi]) {
        _pMinChart.hidden = NO;
        _pCurrentShowView = _pMinChart;
    }else if ([title isEqual:kHZMingXi]) {
        _pMingXiView.hidden = NO;
        _pCurrentShowView = _pMingXiView;
        [_pMingXiView updateNewData:_mingxiCache];
    }else{
        _pKlineChart.hidden = NO;
        _pCurrentShowView = _pKlineChart;
    }
    [self p_isHiddenLandscapeBtn];
}

- (void)p_isHiddenLandscapeBtn
{
    if (_nScreenState == kHZSingleStock_landscape_State) {
        if ([_pCurrentShowView isKindOfClass:[MinChart class]]) {
            [_pMinChart setLandScapeBtnHidden:YES];
        }else if ([_pCurrentShowView isKindOfClass:[KLineChart class]]){
            [_pKlineChart setLandScapeBtnHidden:YES];
        }
    }else{
        if ([_pCurrentShowView isKindOfClass:[MinChart class]]) {
            [_pMinChart setLandScapeBtnHidden:NO];
        }else if ([_pCurrentShowView isKindOfClass:[KLineChart class]]){
            [_pKlineChart setLandScapeBtnHidden:NO];
        }
    }
}

- (void)p_sendInfoWithWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter
{
    if (![objc isKindOfClass:[NSNumber class]]) {
        return;
    }
    kHZApiType nApiType = [objc integerValue];
    switch (nApiType) {
        case kHZApi_MinDetail_Type:
        {
            _mingxiCache = paramter;
            [_pMingXiView updateNewData:_mingxiCache];
        }
            break;
        case kHZApi_Min_Type:
        {
            [_pMinChart updateNewData];
        }
            break;
        case kHZApi_KLine_Type:
        {
            [_pKlineChart updateNewData];
        }
            break;
            
        default:
            break;
    }
}

- (void)p_updateNewConstraints:(kHZSingleStockState)state
{
    _nScreenState = state;
    CGFloat fChartH = CGRectGetHeight(self.frame) - 45 - 45;
    if (state == kHZSingleStock_landscape_State) {
        NSArray *titleArray = [NSArray arrayWithObjects:kHZFenShi,kHZMingXi,kHZDayK,kHZWeekK,kHZMonthK, kHZMinute5,kHZMinute15,kHZMinute30,kHZMinute60, nil];
        [_pRadioGroup setRadioGroup:titleArray];
        [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:NO];
        [_pRadioGroup setSelectedButton:_nMenuState];
        [_pMinChart mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(fChartH);
        }];
        [_pKlineChart mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(fChartH);
        }];
        [_pMingXiView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(fChartH);
        }];
        [_pRadioGroup mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pCurrentShowView.mas_bottom);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(45);
            make.bottom.mas_equalTo(self);
        }];
    }else{
        NSArray *titleArray = [NSArray arrayWithObjects:kHZFenShi,kHZMingXi,kHZDayK,kHZWeekK,kHZMonthK, kHZMinute, nil];
        [_pRadioGroup setRadioGroup:titleArray];
        [_pRadioGroup setHiddenSeparateLine:YES andHiddenUnderline:NO];
        [_pRadioGroup setSelectedButton:_nMenuState];
        [_pMinChart mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pRadioGroup.mas_bottom);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self);
            make.height.mas_equalTo(kChartViewH);
        }];
        [_pKlineChart mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pRadioGroup.mas_bottom);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self);
            make.height.mas_equalTo(kChartViewH);
        }];
        [_pMingXiView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_pRadioGroup.mas_bottom);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self);
            make.height.mas_equalTo(kChartViewH);
        }];
        [_pRadioGroup mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(kMenuH);
        }];
    }
    [self updateConstraintsIfNeeded];
    [self setNeedsLayout];
    [self layoutIfNeeded];
}
#pragma mark -
#pragma mark action
- (void)backViewOnClick
{
    _pBackView.hidden = _pPortraitRadioGroup.hidden = YES;
}
#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_setLandscapeScreen_Action:
        case KMSKlineChartViewType:
        case KMSClickIsShowTenLineActionType:
        {
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZ_radioGroupClick_Action:
        {
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                _nMenuState = btn.tag;
                if (btn.tag == 5) {
                    [self p_loadPortraitRadioGroup];
                }else {
                    [self p_showRelatedView:btn.currentTitle];
                }
                
                //发送日K、周K，月K
                int nKType = -1;
                if ([btn.currentTitle isEqual:kHZDayK]) {
                    nKType = 33;
                }else if ([btn.currentTitle isEqual:kHZWeekK]) {
                    nKType = 39;
                }else if ([btn.currentTitle isEqual:kHZMonthK]) {
                    nKType = 40;
                }
                if (nKType > 0) {
                    [self.parent upToParentWithObjc:self withType:kHZ_ClickKLineRequest_Action withParamter:@(nKType)];
                }
                //通知
                [[NSNotificationCenter defaultCenter] postNotificationName:kRadioGroupClickNotification object:btn userInfo:nil];
            }
            
        }
            break;
        case kHZ_PortraitRadioGroupClick_Action:
        {
            [self backViewOnClick];
            [self p_showRelatedView:nil];
            int nKType = 0;
            if ([paramter isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)paramter;
                nKType += (btn.tag + 35);
                [_pRadioGroup resetSelectedButtonWithTitle:btn.currentTitle andIndex:5];
            }
            [self.parent upToParentWithObjc:self withType:kHZ_ClickKLineRequest_Action withParamter:@(nKType)];
        }
            break;
            
        default:
            break;
    }
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_updateConstraints_Action:
        {
            if ([paramter isKindOfClass:[NSNumber class]]) {
                NSNumber *pNumState = (NSNumber *)paramter;
                [self p_updateNewConstraints:[pNumState integerValue]];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [_pMinChart updateNewData];
                [_pKlineChart updateNewData];
            });
        }
            break;
        case kHZ_cancelLandscapeScreen_Action:
        {
            [_pMinChart downToChildWithObjc:objc withType:type withParamter:paramter];
            [_pKlineChart downToChildWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        case kHZ_updateNewData_Action:
        {
            [self p_sendInfoWithWithObjc:objc withType:type withParamter:paramter];
        }
            break;
            
        default:
            break;
    }
}


@end
